<?php
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$url = $screen->getAjaxLinkUrl('saveEvent', NULL, array('eventId' => $eventId));
$updateId = $screen->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $eventId; ?>() {

        var formData = new FormData($("#post-form-2")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(data) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").load("<?php echo $screen->getLayoutView(); ?>");
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>

<?php
$app = Blyn::app()->getActiveApp();
$typeList = $app->getEventTypeList();
$list = array('0' => '请选择');
$typeList = $list + $typeList;
$model = new BWEventFormModel();
if ($eventId > 0) {
    $event = new BWEvent($eventId);
    $eventType = $event->getGroup(array('parent' => BWEventGroup::Event_TYPE_Promotion));
    if ($eventType)
        $model->eventType = $eventType->getId();
    $model->eventName = $event->name;
    $model->startTime = $event->startTime;
    $model->endTime = $event->endTime;
    $model->description = $event->description;
    $model->image = $event->getMediaManager()->getMedia('image')->content;
}
?>

<div id="myModal<?php echo $eventId; ?>" class="modal hide fade" style="width: 1000px;left: 33%; " tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3>微活动添加</h3>
</div>
<div class="modal-body" id="Event_Add_Body" style="display:block">
    <table class="table table-bordered table-striped">
        <colgroup>
            <col class="span1">
            <col class="span7">
        </colgroup>
        <thead>
            <tr>
                <th>活动属性</th>
                <th>详细内容</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <code>活动类型</code>
                </td>
                <td>
                    <?php
                    echo $form->dropDownList($model, 'eventType', $typeList, array('name' => 'eventType' . $eventId, 'class' => 'span7'));
                    ?>
<!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
                </td>
            </tr>
            <tr><td> <code>活动名称</code> </td>
                <td>
                    <?php
                    echo $form->textField($model, 'eventName', array('name' => "eventName" . $eventId, 'placeholder' => '活动名称...', 'required' => true));
                    ?>
                </td>
            </tr>                
            <tr><td> <code>活动图片</code> </td>
                <td>
                    <?php
                    echo $form->fileField($model, 'image', array('name' => "eventImage" . $eventId,)); // image file select when clicks on upload photo
                    if($eventId > 0) {
                        $imageUrl = Yii::app()->request->baseUrl.$model->image;
                        echo "<img src=".$imageUrl." width=50 height=50 />";
                    }
                    ?>
                    
                </td>
            </tr>
        <script type="text/javascript">
            $(function() {

                $('#datetimepicker1<?php echo $eventId ?>').datetimepicker({
                    pickTime: false
                });
                $('#datetimepicker2<?php echo $eventId ?>').datetimepicker({
                    pickTime: false
                });
            });</script>
        <tr> <td> <code>活动开始时间</code> </td>
            <td>
                <div class="input-append" id="datetimepicker1<?php echo $eventId ?>">
                    <?php
                    echo $form->textField($model, 'startTime', array('name' => "startTime" . $eventId, 'data-format' => "yyyy-MM-dd", 'type' => "text", 'placeholder' => '开始时间...'));
                    ?>
                    <span class="add-on">
                        <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                        </i>
                    </span>
                </div> 
            </td>
        </tr>
        <tr><td> <code>活动结束时间</code></td>
            <td>
                <div class="input-append" id="datetimepicker2<?php echo $eventId ?>">
                    <?php
                    echo $form->textField($model, 'endTime', array('name' => "endTime" . $eventId, 'data-format' => "yyyy-MM-dd", 'type' => "text", 'placeholder' => '结束时间...'));
                    ?>
                    <span class="add-on">
                        <i data-date-icon="icon-calendar" data-time-icon="icon-time" class="icon-calendar">
                        </i>
                    </span>
                </div> 
            </td>
        </tr>
        <tr>
            <td> <code>活动简介</code>  </td>                    
            <td>
                <?php
                echo $form->textArea($model, 'description', array('name' => "description" . $eventId, 'placeholder' => '活动简介...'));
                ?>
        </tr>
        </tbody>
    </table>  
</div>

<div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
    <button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:send<?php echo $eventId; ?>()">保存</button>
</div>
</div>
